/**
 * Created by nickleo on 15/3/5.
 */

var currentTimer;
function timer(times) {
    intDiff = times;
    window.clearInterval(currentTimer)
    currentTimer = window.setInterval(function () {
        var
            minute = 0,
            second = 0;//时间默认值
        if (intDiff >= 0) {
            minute = Math.floor(intDiff / 60);
            second = Math.floor(intDiff) - (minute * 60);
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            $('#minute_show').html('' + minute + '分');
            $('#second_show').html('' + second + '秒');
            intDiff--;
        } else {

            intDiff = $('#autotime').attr("data-time");

            if (intDiff == 0) {
                window.clearInterval(currentTimer)
            }else {
                 timercallback();
            }

        }


    }, 1000);
}

var chartTimer;
function timerCharts(times) {
    intDiff = times;
    window.clearInterval(chartTimer)
    chartTimer = window.setInterval(function () {

        if (intDiff >= 0) {

            intDiff--;
        } else {

            intDiff = $('#chartSelectTime').attr("auto-time");

            if (intDiff == 0) {
                window.clearInterval(currentTimer)
            }else {
                  checkCheckBox();
            }

        }


    }, 1000);
}


/**
 *
 * @param li
 */
function setChartTimeButVal(li) {

    var date_type = $(li).attr("data-type");
    var date_time=$(li).attr("data-time");

 $("#chartSelectTime").attr("data-type",date_type);
 $("#chartSelectTime").attr("data-time",date_time);
 $("#chartSelectTime").text(li.text);

    if(date_type == 'realtime'){
        $('#chartSelectTime').attr("auto-time",6);
        timerCharts($('#chartSelectTime').attr("auto-time"));
    }else {
         $('#chartSelectTime').attr("auto-time",0);
    }

    checkCheckBox();

}


function setTimeButVal(ui) {

    $('#autotime').text(ui.text);
    var sec = $(ui).attr("data-time");
    if (sec == 0) {
        $('#autotime_div').hide();
       // $('#zheshiyigezhanweifu').removeClass("col-lg-9").addClass("col-lg-11")
    }
    else {
        $('#autotime_div').show();
        //$('#zheshiyigezhanweifu').removeClass("col-lg-11").addClass("col-lg-9")
    }
    $('#autotime').attr("data-time", sec);
    timer(sec);

}

function timercallback() {
    var who = $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").attr("data-page");
    var url = null;
    var data = null;

    if (who == "shape.html") {
        url = "/api/shape"
    } else if (who == "topic.html") {
        url = "/api/topic"
    } else if (who == "progress.html") {
        url = "/api/progress"
        var selected = $("#group_names  option:selected");
        data = {"group":selected.val()}
    }else {
        alert("Why don't you drive  ");
    }
    if (url != null) {

        http_get(url, data, refreshContent)
    }

}

/**
 * 刷新页面内容, 先清除,然后根据结果添加新的
 * @param who
 * @param data
 */

function refreshContent(data) {
    var who = $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").attr("data-page");

    if (data != null && who == "shape.html") {
        $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").empty();
        $.each(data, function (idx, shapegroup) {
            var group = createShapeGroup(shapegroup);
            var row = createshapeRow(shapegroup);
            $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").append(group).append(row);
        });
    } else if (data != null && who == "topic.html") {
         $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").empty();
        $.each(data, function (idx, treegroup) {
            var group = createTreeGroup(treegroup);
            var row = createTreeRow(treegroup);
            $("#neirongdemingziyidingyaohenchangcainengfangzhibeiwucaozuoxiugai").append(group).append(row);
        });

    }
    else if (data != null && who == "progress.html") {
         $("#process_tbody").empty();
        $.each(data, function (idx, line) {
            var row = createTableline(line);
            $("#process_tbody").append(row);
        });

    }

}

function http_get(url, args, callback) {
    $.ajax({
        type: "get",//使用get方法访问后台
        dataType: "json",//返回json格式的数据
        url: url,//要访问的后台地址
        data: args,
        success: callback
    });

}


